home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_01_05
/
1n05020a
< prev
next >
Wrap
Text File
|
1990-03-25
|
2KB
|
72 lines
/* Listing 3
findtext.c: Locate places in file with a pattern
Written by Hugh Staley
Box 842
Los Alamos, NM 87544
*/
#include <stdio.h>
char Match [100] = {"FILE BLOCK"};
void Search (fd)
FILE *fd;
{
int Count = 0, Ch, Length;
long Offset;
Length = strlen (Match);
while ((Ch = getc (fd)) != EOF)
if (Count < Length)
if (Ch == Match [Count])
{
++Count;
if (Count == 1)
Offset = ftell (fd) - 1;
else if (Count == Length)
printf ("%lX: %s", Offset, Match);
}
else
Count = 0;
else
if (Ch == '\0')
{
printf (": %d bytes\n", ++Count);
Count = 0;
}
else
{
++Count;
printf ("%c", Ch);
}
} /* Search file */
main (argc, argv)
int argc;
char *argv [];
{
FILE *fd;
if (argc < 2 || argc > 3)
{
printf ("usage: findtext [-tTEXT] fn\n");
exit (0);
}
if (strncmp (argv [1], "-t", 2) == 0)
strcpy (Match, argv [1] + 2);
if ((fd = fopen (argv [argc - 1], "rb")) != NULL)
{
Search (fd);
fclose (fd);
}
else
printf ("Cannot open \"%s\".\n", argv [argc - 1]);
}